<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div style="height: 400px">
      <img
        class="lazy-load"
        data-url="https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
      />
    </div>
    <div style="height: 400px">
      <img
        class="lazy-load"
        data-url="https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
      />
    </div>
    <div style="height: 400px">
      <img
        class="lazy-load"
        data-url="https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
      />
    </div>
    <div style="height: 400px">
      <img
        class="lazy-load"
        data-url="https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
      />
    </div>
    <div style="height: 400px">
      <img
        class="lazy-load"
        data-url="https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
      />
    </div>
  </body>
  <script>
    const imageElements = document.querySelectorAll(".lazy-load");

    const observe = new IntersectionObserver(
      (entries, observer) => {
        entries.forEach((item) => {
          console.log(item.isIntersecting, 888);

          if (item.isIntersecting) {
            item.target.src = item.target.dataset.url;
          }
        });
      },
      {
        root: null, // 使用视口作为根元素
        rootMargin: "0px",
        threshold: 0.1, // 只有元素可见 10% 时才触发
      }
    );

    imageElements.forEach((item) => {
      observe.observe(item);
    });
  </script>
</html>
